# stop apps
echo "Stop ALL apps ..."
for pid in $(ps -ef | grep "java" | awk '/pengxin-[a|s]/ {print $2}'); do kill -TERM $pid; done

# go to release dir
pushd ~/release
# keep last 4 release dir
rm -rf `ls -t | grep "v20" | tail -n  +5`
echo "Remove softlink ..."
rm -f current_dev

echo "Create new release dir ..."
a=`date +"%Y%m%d.%H%M%S"`
dir_name="v$a"
mkdir $dir_name
cp /root/pengxin/deploy/server/pengxin-api/target/pengxin-api.jar       $dir_name/
cp /root/pengxin/deploy/server/pengxin-admin-api/target/pengxin-admin-api.jar       $dir_name/
echo "Create softlink ..."
ln -s $dir_name current_dev
popd

# start apps

echo "Start prod apps ..."
pushd ~/release
nohup java -javaagent:/root/middleware/apache-skywalking-apm-bin/agent/skywalking-agent.jar -DSW_AGENT_NAME=pengxin-app-api -DSW_AGENT_COLLECTOR_BACKEND_SERVICES=172.19.160.34:11800 -jar ~/release/current_dev/pengxin-api.jar --spring.profiles.active=prod  2>&1 > api-prod.log &
nohup java -javaagent:/root/middleware/apache-skywalking-apm-bin/agent/skywalking-agent.jar -DSW_AGENT_NAME=pengxin-admin-api -DSW_AGENT_COLLECTOR_BACKEND_SERVICES=172.19.160.34:11800 -jar ~/release/current_dev/pengxin-admin-api.jar --spring.profiles.active=prod  2>&1 > admin-api-prod.log &

popd

# done!
echo "Done. Please wait."
